package com.springbootweb.controller;

//import com.springbootribbon.service.HelloService;
import com.codingapi.txlcn.tc.annotation.DTXPropagation;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.springbootweb.client.HelloClient;
import com.springbootweb.client.HelloClient1;
//import com.springbootweb.common.Sender;
import com.springbootweb.common.UuidUtil;
import com.springbootorder.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Api(description = "Test hello class")
@Controller
@RequestMapping("helloController")
public class HelloController {
    @Autowired
    HelloClient helloClient;
    @Autowired
    HelloClient1 helloClient1;
    @Value("${spring.application.name}")
    private String servername;

//    @Autowired
//    Sender sender;

    @ApiOperation(value = "返回测试主页", notes="返回测试主页")
    @RequestMapping("/index")
    public String index(){
        return "X-admin/login";
    }

    @ApiOperation(value = "通过传递过来的参数在页面打印出来", notes="通过传递过来的参数在页面打印出来")
    @ApiImplicitParam(name = "name", value = "参数", paramType = "query", required = true, dataType = "String")
    @GetMapping("/hello")
    @ResponseBody
    public String helloTest(String name){
        return "传递过来的姓名是："+name;
    }

    @ApiOperation(value = "调用其他服务，通过传递过来的参数获取返回值在页面打印出来", notes="调用其他服务，通过传递过来的参数获取返回值在页面打印出来")
    @ApiImplicitParam(name = "name", value = "参数", paramType = "query", required = true, dataType = "String")
    @GetMapping("/hello1")
    @ResponseBody
    public String helloTest1(String name){
        return helloClient.sayHello(name);
    }

    @ApiOperation(value = "调用其他服务，返回List集合", notes="调用其他服务，返回List集合")
    @ApiImplicitParam
    @GetMapping("/hello2")
    @ResponseBody
    public List<User> helloTest2(){
        List<User> test = helloClient.test();
        return test;
    }
    @ApiOperation(value = "测试MQ", notes="测试MQ")
    @ApiImplicitParam
    @GetMapping("/hello3")
    @ResponseBody
    public String helloTest3(){
        System.out.println("send string:hello world");
//        sender.send("hello world");
        return "sending...";
    }
//    @Transactional
//    @LcnTransaction(propagation = DTXPropagation.REQUIRED) //分布式事务注解
    @ApiOperation(value = "测试分布式事物", notes="测试分布式事物")
    @GetMapping("/saveOrUpdate")
    public void saveOrUpdate(){
        try {
            User user = new User();
            user.setId(UuidUtil.getstring());
            user.setAge(55);
            user.setLoginName("888");
            user.setName(UuidUtil.getint());
            user.setSex(1);
            user.setPassword(UuidUtil.getint());
            helloClient1.update();
            helloClient.save(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
